﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Imladris.Entities;
using Imladris.UI.Repositories.Interfaces;
using Imladris.UI.Repositories.WCFRepositories.DocumentService;

namespace Imladris.UI.Repositories.WCFRepositories
{
    public class DocumentWCFRepo : IDocumentsRepository
    {
        private DocServiceClient _proxy;
       
        public DocumentWCFRepo()
        {
            _proxy = new DocServiceClient();
        }

        public List<Document> GetDocumentList()
        {
            var client = new DocServiceClient();
            var result = client.GetAllDocuments();
           
            List<Document> documents = new List<Document>();
            CreateDocumentDTOToDocumentMap();

            foreach (var documentDto in result)
            {
                var doc = Mapper.Map<Document>(documentDto);
                documents.Add(doc);
            }

            return documents;
        }

        private void CreateDocumentDTOToDocumentMap()
        {
            Mapper.CreateMap<DocumentDTO, Document>();
            Mapper.CreateMap<DocumentChangeDTO, DocumentChange>();
            Mapper.CreateMap<DocumentPersonDTO, DocumentPerson>();
            Mapper.CreateMap<DocumentRightDTO, DocumentRight>();
            Mapper.CreateMap<DocumentVersionDTO, DocumentVersion>();
        }

        private void CreateDocumentToDocumentDTOMap()
        {
            Mapper.CreateMap<Document, DocumentDTO>();
            Mapper.CreateMap<DocumentChange, DocumentChangeDTO>();
            Mapper.CreateMap<DocumentPerson, DocumentPersonDTO>();
            Mapper.CreateMap<DocumentRight, DocumentRightDTO>();
            Mapper.CreateMap<DocumentVersion, DocumentVersionDTO>();
        }
    }
}
